import { createRouter, createWebHashHistory } from 'vue-router'
/* Layout */
import Layout from '@/layout'

const defaultList = [{
  path: 'index',
  component: () => import('@/views/base/dashboard/index.vue'),
  name: 'dashboard',
  meta: {
    title: '首页',
    icon: 'dashboard',
    noCache: true,
    affix: true
  }
}]

const routes = [{
  path: '/redirect',
  component: Layout,
  meta: {
    hidden: true
  },
  children: [{
    path: '/redirect/:path(.*)',
    component: () => import('@/views/base/redirect.vue')
  }]
},
{
  path: '/login',
  name: 'login',
  component: () => import('@/views/base/login/index.vue'),
  meta: {
    hidden: true,
    noCache: true
  }
},
{
  path: '/404',
  name: '404',
  component: () => import('@/views/base/error/404.vue'),
  meta: {
    hidden: true,
    noCache: true
  }
},
{
  path: '/',
  component: Layout,
  redirect: 'index',
  meta: {
    hidden: false
  },
  children: defaultList
},
{
  path: '/example',
  component: Layout,
  redirect: '/example/table',
  name: 'Example',
  meta: { title: '示例', icon: 'code' },
  children: [
    {
      path: 'table',
      name: 'Table',
      component: () => import('@/views/table/index'),
      meta: { title: '表格', icon: 'table' }
    },
    {
      path: 'tree',
      name: 'Tree',
      component: () => import('@/views/tree/index'),
      meta: { title: '树', icon: 'tree' }
    }
  ]
},
{
  path: '/form',
  component: Layout,
  children: [
    {
      path: 'index',
      name: 'Form',
      component: () => import('@/views/form/index'),
      meta: { title: '表单', icon: 'form' }
    }
  ]
}]

const routerParams = {
  history: createWebHashHistory(process.env.VUE_APP_BASE_API),
  routes,
  scrollBehavior() {
    return { left: 0, top: 0 }
  }
}

const router = createRouter(routerParams)

// 去除重复路由报错的问题
const originalPush = router.push
router.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export function resetRouter() {
  const newRouter = createRouter(routerParams)
  router.resolve = newRouter.resolve
}

export default router
